<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns:MSHelp="http://www.microsoft.com/MSHelp/" lang="en-us" xml:lang="en-us"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="DC.Type" content="topic">
<meta name="DC.Title" content="Scheduler Bypass">
<meta name="DC.subject" content="Scheduler Bypass">
<meta name="keywords" content="Scheduler Bypass">
<meta name="DC.Relation" scheme="URI" content="../tbb_userguide/Useful_Task_Techniques.htm">
<meta name="DC.Relation" scheme="URI" content="Continuation_Passing.htm#tutorial_Continuation_Passing">
<meta name="DC.Relation" scheme="URI" content="Recycling.htm#tutorial_Recycling">
<meta name="DC.Relation" scheme="URI" content="How_Task_Scheduling_Works.htm#tutorial_How_Task_Scheduling_Works">
<meta name="DC.Format" content="XHTML">
<meta name="DC.Identifier" content="tutorial_Scheduler_Bypass">
<link rel="stylesheet" type="text/css" href="../intel_css_styles.css">
<title>Scheduler Bypass</title>
<xml>
<MSHelp:Attr Name="DocSet" Value="Intel"></MSHelp:Attr>
<MSHelp:Attr Name="Locale" Value="kbEnglish"></MSHelp:Attr>
<MSHelp:Attr Name="TopicType" Value="kbReference"></MSHelp:Attr>
</xml>
</head>
<body id="tutorial_Scheduler_Bypass">
 <!-- ==============(Start:NavScript)================= -->
 <script src="..\NavScript.js" language="JavaScript1.2" type="text/javascript"></script>
 <script language="JavaScript1.2" type="text/javascript">WriteNavLink(1);</script>
 <!-- ==============(End:NavScript)================= -->
<a name="tutorial_Scheduler_Bypass"><!-- --></a>

 
  <h1 class="topictitle1">Scheduler Bypass</h1>
 
   
  <div> 
	 <p>Scheduler bypass is an optimization where you directly specify the next
		task to run. Continuation-passing style often opens up an opportunity for
		scheduler bypass. For example, at the end of the continuation-passing example
		in the previous section, method 
		<samp class="codeph">execute()</samp> spawns task "a" and returns. By the execution
		rules in 
		<strong>How Task Scheduling Works</strong>, that sequence causes the executing
		thread to do the following: 
	 </p>
 
	 <ol> 
		<li> 
		  <p>Push task "<samp class="codeph">a</samp>" onto the thread's deque. 
		  </p>
 
		</li>
 
		<li> 
		  <p>Return from method 
			 <samp class="codeph">execute()</samp>. 
		  </p>
 
		</li>
 
		<li> 
		  <p>Pop task "<samp class="codeph">a</samp>" from the thread's deque, unless it is
			 stolen by another thread. 
		  </p>
 
		</li>
 
	 </ol>
 
	 <p>Steps 1 and 3 introduce unnecessary deque operations, or worse yet,
		permit stealing that can hurt locality without adding significant parallelism.
		Method 
		<samp class="codeph">execute()</samp>can avoid these problems by returning a pointer
		to 
		<samp class="codeph">a</samp> instead of spawning it. When using the method shown in
		
		<strong>How Task Scheduling Works</strong>, 
		<samp class="codeph">a</samp> becomes the next task executed by the thread.
		Furthermore, this approach guarantees that the thread executes 
		<samp class="codeph">a</samp>, not some other thread.
		
	 </p>
 
	 <p>The following example shows the changes to the example in the previous
		section in 
		<samp class="codeph"><span style="color:blue"><strong>bold font</strong></span></samp>: 
	 </p>
 
	 <pre>struct FibTask: public task {
    ...
    task* execute() {
        if( n&lt;CutOff ) {
            *sum = SerialFib(n);
            return NULL;
        } else {
            FibContinuation&amp; c = 
                *new( allocate_continuation() ) FibContinuation(sum);
&nbsp;
            FibTask&amp; a = *new( c.allocate_child() ) FibTask(n-2,&amp;c.x);
            FibTask&amp; b = *new( c.allocate_child() ) FibTask(n-1,&amp;c.y);
            // Set ref_count to "two children".
            c.set_ref_count(2);
            spawn( b );
            // spawn( a ); This line removed
            // return NULL; This line removed
            <span style="color:blue"><strong>return &amp;a;</strong></span>
        }
    }
};</pre> 
  </div>
 
  
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong>&nbsp;<a href="../tbb_userguide/Useful_Task_Techniques.htm">Useful Task Techniques</a></div>
</div>
<div class="See Also">
<h2>See Also</h2>
<div class="linklist">
<div><a href="Continuation_Passing.htm#tutorial_Continuation_Passing">Continuation Passing 
		  </a></div>
<div><a href="Recycling.htm#tutorial_Recycling">Recycling 
		  </a></div>
<div><a href="How_Task_Scheduling_Works.htm#tutorial_How_Task_Scheduling_Works">How Task Scheduling Works 
		  </a></div></div>
</div> 

</body>
</html>
